package com.leetcode.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MedianOfTwoSortedArrays {

	public static void main(String[] args){
		
		int nums1[]={1,3};
		int nums2[]={2};
		double result=findMedianSortedArrays(nums1, nums2);
		System.out.println(result);
		
		
	}
	
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        List<Integer> list=new ArrayList<Integer>();
        
        List<Integer> lista= Arrays.asList(parseIntToInterger(nums1));
        List<Integer> listb=Arrays.asList(parseIntToInterger(nums2));
        list.addAll(lista);
        list.addAll(listb);
        Collections.sort(list);
        int len=list.size();
        if(list==null||len==0) return 0;
        int mid=(len-1)/2;
        double result=0;
        if(len%2==0){
        	result=((double)list.get(mid)+list.get(mid+1))/2;
        }else{
        	result=list.get(mid);
        }
        return result;
    }
    
    public static Integer[] parseIntToInterger(int[] nums){
    	Integer[] intNums=new Integer[nums.length];
    	for(int i=0;i<nums.length;i++){
    		intNums[i]=Integer.valueOf(nums[i]);
    	}
    	return intNums;
    }
}